package com.demo.spring.formework.webmvc.servlet;

import com.demo.spring.formework.annotation.QBRequestMapping;
import com.demo.spring.formework.annotation.QBRequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class QBHandlerAdapter {
    public boolean supports(Object handler) {
        return (handler instanceof QBHandlerMapping);
    }

    public QBModelAndView handler(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {
        QBHandlerMapping handlerMapping = (QBHandlerMapping) handler;

        Map<String, Integer> paramMapping = new HashMap<String, Integer>();

        Annotation[] [] pa = handlerMapping.getMethod().getParameterAnnotations();
        for (int i = 0; i < pa.length ; i ++) {
            for(Annotation a : pa[i]){
                if(a instanceof QBRequestParam){
                    String paramName = ((QBRequestParam) a).value();
                    if(!"".equals(paramName.trim())){
                        paramMapping.put(paramName, i);
                    }
                }
            }
        }
        //处理Reques和Response
        Class<?>[] paramTypes = handlerMapping.getMethod().getParameterTypes();
        for (int i = 0; i < paramTypes.length; i++) {
            Class<?> type = paramTypes[i];
            if (type == HttpServletRequest.class || type == HttpServletResponse.class) {
                paramMapping.put(type.getName(), i);
            }
        }
        //2.拿到自定义参数位置，用户通过URL传过来的参数列表
        Map<String, String[]> reqParameterMap = req.getParameterMap();
        //3.构造实参表
        Object[] paramValues = new Object[paramTypes.length];

        for (Map.Entry<String, String[]> param : reqParameterMap.entrySet()) {
            String value = Arrays.toString(param.getValue()).replaceAll("\\[|\\]", "").replaceAll("\\s", "");

            if (!paramMapping.containsKey(param.getKey())) {
                continue;
            }

            int index = paramMapping.get(param.getKey());

            paramValues[index] = caseStringValue(value, paramTypes[index]);
        }

        if (paramMapping.containsKey(HttpServletRequest.class.getName())) {
            int reqIndex = paramMapping.get(HttpServletRequest.class.getName());
            paramValues[reqIndex] = req;
        }

        if (paramMapping.containsKey(HttpServletResponse.class.getName())) {
            int respIndex = paramMapping.get(HttpServletResponse.class.getName());
            paramValues[respIndex] = resp;
        }
        //4.从handler中取出controller,method ,然后利用反射机制进行调用
        Object result = handlerMapping.getMethod().invoke(handlerMapping.getController(), paramValues);

        if (result == null) {
            return null;
        }
        boolean isModelAndView = handlerMapping.getMethod().getReturnType() ==
                QBModelAndView.class;
        if (isModelAndView) {
            return (QBModelAndView) result;
        } else {
            return null;
        }

    }



    private Object caseStringValue(String value, Class<?> clazz) {
        if(clazz == String.class){
            return value;
        }else if(clazz == Integer.class){
            return Integer.valueOf(value);
        }else if(clazz == int.class){
            return Integer.valueOf(value).intValue();
        }else {
            return null;
        }
    }



}
